package server.business.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import server.business.mapper.*;
import server.business.pojo.*;
import server.business.service.*;
import server.business.utils.GenerateNumberBySnowFlake;
import server.business.utils.PageResult;
import server.business.utils.Result;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.beans.Beans;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author lmj
 * @date 2022/8/21
 */
@Controller
@RequestMapping("/bloodDispense")
public class BloodDispenseController {
    @Autowired
    private BloodDispenseService bloodDispenseService;
    @Autowired
    private PatientService patientService;
    @Autowired
    private BloodSamplePatientService bloodSamplePatientService;
    @Autowired
    private OverallBloodService overallBloodService;
    @Autowired
    private BloodInformationService bloodInformationService;
    @Autowired
    private BloodTestService bloodTestService;
    @Autowired
    private BloodDispenseMapper bloodDispenseMapper;
    @Autowired
    private BloodSamplePatientMapper bloodSamplePatientMapper;
    @Autowired
    private TransfusionApplicationInformationService transfusionApplicationInformationService;
    @Autowired
    private TransfusionApplicationInformationMapper transfusionApplicationInformationMapper;


    @Autowired
    private BloodSampleService bloodSampleService;
    @Autowired
    private BloodTestMapper bloodTestMapper;
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private BloodSampleMapper bloodSampleMapper;
    @Autowired
    private BloodInformationMapper bloodInformationMapper;
    @Autowired
    private BloodDispenseReviewService bloodDispenseReviewService;



    /**
     * 获取页面
     * 配血分页查询
     */
    @RequestMapping("/normal/listByPage")
    @ResponseBody
    public PageResult getPagBloodDispense(HttpServletRequest request, HttpServletResponse response) {
        Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        Page<BloodDispenseVo> page = new Page<>(currentPage,pageSize);
        QueryWrapper<BloodDispenseVo> queryWrapper=new QueryWrapper<>();
        IPage<BloodDispenseVo> samplePage = bloodDispenseMapper.selectBloodDispenseByPage(page,queryWrapper);
        PageResult pageResult = new PageResult("200", "成功", samplePage.getRecords(), currentPage, pageSize,
                samplePage.getTotal(), samplePage.getPages());
        List<BloodDispenseVo> bloodDispenseVo=page.getRecords();
        return pageResult;
    }

    /**
     * 根据检验单号查找检验信息
     * @param bloodDispenseVo
     * @return
     */
    @RequestMapping("/normal/getReturnTestInformation")
    @ResponseBody
    public Result getReturnTestInformation(@RequestBody BloodDispenseVo bloodDispenseVo){
        BloodTest bloodTest=bloodTestService.getOne(new QueryWrapper<BloodTest>().eq("inspection_sheet",
                bloodDispenseVo.getInspectionSheet()));
        //根据patientId找到对应病历号和姓名
        Patient patient=patientService.getById(bloodTest.getPatientId());
        bloodDispenseVo.setMedicalRecordNumber(patient.getMedicalRecordNumber());
        bloodDispenseVo.setName(patient.getName());
        bloodDispenseVo.setBloodType(bloodTest.getBloodType());
        bloodDispenseVo.setRhd(bloodTest.getRhd());
        //根据patient_id找到对应患者血样信息
        BloodSamplePatient bloodSamplePatient=
                bloodSamplePatientMapper.selectDispenseByPatientId(bloodTest.getPatientId());
//        //患者血液样本编号
        bloodDispenseVo.setBloodSampleNumber(bloodSamplePatient.getBloodSampleNumber());
        Result result=new Result();
        result.setData(bloodDispenseVo);
        return result;
    }
    /**
     * 配血登记
     * @return {@link Result}
     */

    @RequestMapping("/normal/add")
    @ResponseBody
    public Result addBloodDispense(@RequestBody BloodDispenseVo bloodDispenseVo) {
        System.out.println(bloodDispenseVo.getCost()+"好好规划局");
        //自动生成验血单号
        GenerateNumberBySnowFlake generateNumberBySnowFlake=new GenerateNumberBySnowFlake(10,"PX");
        String dispenseNumber=generateNumberBySnowFlake.genNumberByflag();
        bloodDispenseVo.setNumber(dispenseNumber);
        //存要添加的记录
        List<BloodDispense> bloodDispenseList= new ArrayList<>();

        System.out.println(bloodDispenseVo);
        //        根据检验单号找到病人
        BloodTest bloodTest=bloodTestService.getOne(new QueryWrapper<BloodTest>().eq("inspection_sheet",
                bloodDispenseVo.getInspectionSheet()));
        System.out.println(bloodTest.getPatientId());
        List<TransfusionApplicationInformation> transfusionApplication=
                transfusionApplicationInformationService.list(new QueryWrapper<TransfusionApplicationInformation>().eq(
                        "patient_id",
                        bloodTest.getPatientId()).eq("is_deleted",0).eq("dispense_flag","0"));
        System.out.println(transfusionApplication.size());
        //存分配的血液编号
        Result result=new Result();
        List<BloodSample> bloodSampleList=new ArrayList<>();
        for (int i = 0; i <transfusionApplication.size() ; i++) {
            //分配的血液样本
            BloodSample bloodSample=bloodSampleService.getOne(new QueryWrapper<BloodSample>().eq("blood_number",
                    transfusionApplication.get(i).getBloodNumber()));
            //bloodDispenseVo
            bloodDispenseVo.setBloodTestId(bloodTest.getId());
            bloodDispenseVo.setPatientId(bloodTest.getPatientId());
//            bloodDispenseVo.setGmtCreate(new Date());
            bloodDispenseVo.setStatue("1");
            bloodDispenseVo.setIsDeleted(0);
            bloodDispenseVo.setDispenseRegisterTime(new Date());
            bloodDispenseVo.setBloodSampleId(bloodSample.getId());
            //状态该成配血
            bloodSample.setState("004");
            //找到血液基本信息，状态改成配血
            BloodInformation bloodInformation=bloodInformationMapper.selectByBloodNumber(bloodSample.getBloodNumber());
            bloodInformation.setState("10");
            //根据患者血样编号找到患者血样
            BloodSamplePatient bloodSamplePatient=
                    bloodSamplePatientService.getOne(new QueryWrapper<BloodSamplePatient>().eq("blood_sample_number",
                            bloodDispenseVo.getBloodSampleNumber()));
            bloodSamplePatient.setIsInvalid(1);
            transfusionApplication.get(i).setDispenseFlag("1");
            transfusionApplication.get(i).setState("1");

            //拷贝成BloodDispense
            BloodDispense bloodDispense=new BloodDispense();
            BeanUtils.copyProperties(bloodDispenseVo,bloodDispense);
            bloodDispenseList.add(bloodDispense);

            bloodSampleService.updateById(bloodSample);
            bloodInformationService.updateById(bloodInformation);
            bloodSamplePatientService.updateById(bloodSamplePatient);
            transfusionApplicationInformationService.updateById(transfusionApplication.get(i));
        }
        System.out.println(bloodDispenseList.size()+"配血列表");
        boolean success=bloodDispenseService.saveBatch(bloodDispenseList);
        for(int i=0;i<bloodDispenseList.size();i++){
            //配血审核登记
            System.out.println("好哥哥黑胡椒"+bloodDispenseList.get(i).getId());
            BloodDispenseReview bloodDispenseReview=new BloodDispenseReview();
            bloodDispenseReview.setBloodDispenseId(bloodDispenseList.get(i).getId());
            bloodDispenseReview.setState("1");
//            bloodDispenseReview.setApplicantId(Long.parseLong(bloodDispenseList.get(i).getDispenseRegisterNumber()));
            bloodDispenseReview.setApplicantId(bloodDispenseList.get(i).getDispenseRegisterNumber());
            bloodDispenseReview.setApplyTime(bloodDispenseList.get(i).getDispenseRegisterTime());
            bloodDispenseReviewService.save(bloodDispenseReview);
        }
        if(success){
            result.setCode("200");
            result.setMsg("插入样本成功");
        } else if(transfusionApplication.size()==0){
            result.setCode("500");
            result.setMsg("添加失败,该患者暂未分配配血血液！！！");
        }else{
            result.setCode("500");
            result.setMsg("添加失败");
        }
        return result;

    }
        /**
     * 配血结果登记
     * @param bloodDispenseVo
     * @return
     */
    @PostMapping("/normal/addResult")
    @ResponseBody
    public Result resultBloodDispense(@RequestBody BloodDispenseVo bloodDispenseVo) {
        BloodSample bloodSample=bloodSampleMapper.selectByNumber(bloodDispenseVo.getBloodNumber());
        BloodInformation bloodInformation=bloodInformationMapper.selectByBloodNumber(bloodSample.getBloodNumber());

        TransfusionApplicationInformation transfusionApplicationInformation=
                transfusionApplicationInformationMapper.selectByBloodNumber(bloodDispenseVo.getPatientId(),
                        bloodInformation.getBloodId());

        if(bloodDispenseVo.getMainSideResult().equals("01")==false||bloodDispenseVo.getPrimarySideResult().equals(
                "01")==false){
            bloodDispenseVo.setStatue("7");
            transfusionApplicationInformation.setState("7");
        }else{
            bloodDispenseVo.setStatue("2");
            transfusionApplicationInformation.setState("2");
        }
        bloodDispenseVo.setDispenseTime(new Date());
        Result result = new Result();
        //拷贝成BloodDispense
        BloodDispense bloodDispense=new BloodDispense();
        BeanUtils.copyProperties(bloodDispenseVo,bloodDispense);
        boolean success = bloodDispenseService.updateById(bloodDispense);
        transfusionApplicationInformationService.updateById(transfusionApplicationInformation);
        if(success) {
            result.setCode("200");
            result.setMsg("登记成功");
        }else{
            result.setCode("500");
            result.setMsg("登记失败");
        }
        return result;
    }
        /**
     * 删除通过id
     */
    @RequestMapping("/normal/deleteById")
    @ResponseBody
    public Result deleteBloodDispense(@RequestBody BloodDispense bloodDispense) {
        boolean success = bloodDispenseService.removeById(bloodDispense.getId());
        Result result = new Result();
        if (success) {
            result.setCode("200");
            result.setMsg("删除成功");
        } else {
            result.setCode("500");
            result.setMsg("删除失败");
        }
        return result;
    }
    /**
     * 批量删除
     */
    @RequestMapping("/normal/batchDelete")
    @ResponseBody
    public Result batchDeleteBloodDispense(@RequestBody ArrayList<Long> ids) {
        System.out.println(ids);
        boolean success = bloodDispenseService.removeByIds(ids);
        Result result = new Result();
        if (success) {
            result.setCode("200");
            result.setMsg("删除成功");
        } else {
            result.setCode("500");
            result.setMsg("删除失败");
        }
        return result;
    }


        /**
     * 修改
     * @param bloodDispenseVo
     * @return
     */
    @RequestMapping("/normal/update")
    @ResponseBody
    public Result updateBloodDispense(@RequestBody BloodDispenseVo bloodDispenseVo) {
        Result result = new Result();
//        bloodDispenseVo.setGmtModified(new Date());
        BloodSample bloodSample=bloodSampleMapper.selectByNumber(bloodDispenseVo.getBloodNumber());
        BloodInformation bloodInformation=bloodInformationMapper.selectByBloodNumber(bloodSample.getBloodNumber());

        TransfusionApplicationInformation transfusionApplicationInformation=
                transfusionApplicationInformationMapper.selectByBloodNumber(bloodDispenseVo.getPatientId(),
                        bloodInformation.getBloodId());

        if(bloodDispenseVo.getMainSideResult().equals("01")==false||bloodDispenseVo.getPrimarySideResult().equals(
                "01")==false){
            bloodDispenseVo.setStatue("7");
            transfusionApplicationInformation.setState("7");
        }else{
            bloodDispenseVo.setStatue("2");
            transfusionApplicationInformation.setState("2");
        }
        //拷贝成BloodDispense
        BloodDispense bloodDispense=new BloodDispense();
        BeanUtils.copyProperties(bloodDispenseVo,bloodDispense);
        //录入数据库
        boolean success = bloodDispenseService.updateById(bloodDispense);
        transfusionApplicationInformationService.updateById(transfusionApplicationInformation);
        if(success) {
            result.setCode("200");
            result.setMsg("修改成功");
        }else{
            result.setCode("500");
            result.setMsg("修改失败");
        }
        return result;
    }


    /**
     * 搜索
     *
     * @param request  请求
     * @param response 响应
     * @return {@link PageResult}
     */
    @RequestMapping("/normal/search")
    @ResponseBody
    public PageResult searchBloodDispense(HttpServletRequest request, HttpServletResponse response) {
        //查询条件
        String number = request.getParameter("number");
        String inspectionSheet = request.getParameter("inspectionSheet");
        String medicalRecordNumber = request.getParameter("medicalRecordNumber");
        String name = request.getParameter("name");
        String bloodSampleNumber = request.getParameter("bloodSampleNumber");
        String bloodNumber = request.getParameter("bloodNumber");
        String dispenseMethod = request.getParameter("dispenseMethod");
        String dispenseRegister = request.getParameter("dispenseRegister");
        String dispenseRegisterNumber = request.getParameter("dispenseRegisterNumber");
        String dispensePeople = request.getParameter("dispensePeople");
        String dispensePeopleNumber = request.getParameter("dispensePeopleNumber");
        String reviewerNumber = request.getParameter("reviewerNumber");
        String reviewerName = request.getParameter("reviewerName");
        String startRegisterTime = request.getParameter("startRegisterTime");
        String endRegisterTime = request.getParameter("endRegisterTime");
        String startDispenseTime = request.getParameter("startDispenseTime");
        String endDispenseTime = request.getParameter("endDispenseTime");
        String startReviewTime = request.getParameter("startReviewTime");
        String endReviewTime = request.getParameter("endReviewTime");
        String statue = request.getParameter("statue");
        Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));

        Page<BloodDispenseVo> page = new Page<>(currentPage, pageSize);
//        //创建查询条件
        QueryWrapper<BloodDispenseVo> queryWrapper = new QueryWrapper<>();
        //单据号
        if (number != "")
            queryWrapper.like("a.number", number);
        //检验单号
        if (inspectionSheet != "")
            queryWrapper.eq("inspection_sheet", inspectionSheet);
        //病历号
        if (medicalRecordNumber != ""){
            queryWrapper.like("medical_record_number", medicalRecordNumber);
        }
        //姓名
        if (name != "")
            queryWrapper.eq("name",name);
        //血液样本编号
        if (bloodSampleNumber != "")
            queryWrapper.like("blood_sample_number", bloodSampleNumber);
        //血液样本编号
//        if (bloodNumber != "")
//            queryWrapper.eq("blood_sample_id", bloodSample.getId());
        //配血基本信息登记人工号
        if (dispenseRegisterNumber != "")
            queryWrapper.like("dispense_register_number", dispenseRegisterNumber);
        //配血基本信息登记人
        if (dispenseRegister != "")
            queryWrapper.like("dispense_register", dispenseRegister);
        //配血基本信息登记时间
        if(startRegisterTime != "")
            queryWrapper.ge("dispense_register_time", startRegisterTime);
        if(endRegisterTime != "")
            queryWrapper.le("dispense_register_time", endRegisterTime);
        //配血人工号
        if (dispensePeopleNumber != "")
            queryWrapper.like("dispense_people_number", dispensePeopleNumber);
        //配血人
        if (dispensePeople != "")
            queryWrapper.like("dispense_people", dispensePeople);
        //配血时间
        if(startDispenseTime != "")
            queryWrapper.ge("dispense_time", startDispenseTime);
        if(endDispenseTime != "")
            queryWrapper.le("dispense_time", endDispenseTime);
        //审核人工号
        if (reviewerNumber != "")
            queryWrapper.like("a.reviewer_number", reviewerNumber);
        //审核人
        if (reviewerName != "")
            queryWrapper.like("a.reviewer_name", reviewerName);
        //审核时间
        if(startReviewTime != "")
            queryWrapper.ge("a.review_time", startReviewTime);
        if(endReviewTime != "")
            queryWrapper.le("a.review_time", endReviewTime);
        //配血方法
        if (dispenseMethod!= "")
            queryWrapper.eq("dispense_method", dispenseMethod);
        //状态
        if (statue!= "")
            queryWrapper.eq("statue", statue);

        BloodDispenseVo bloodDispenseVo=new BloodDispenseVo();
        //查询数据库
        bloodDispenseMapper.selectBloodDispenseByPage(page, queryWrapper);

        //返回的数据
        PageResult sampleDataVo = new PageResult("200", "成功", page.getRecords(), currentPage, pageSize, page.getTotal(), page.getPages());
        sampleDataVo.setCode("200");
        sampleDataVo.setMsg("搜索成功");
        sampleDataVo.setData(page.getRecords());
        System.out.println(page.getRecords());
        return sampleDataVo;

    }

    /**
     * 下载excel
     *
     * @param request  请求
     * @param response 响应
     */
    @RequestMapping("/normal/downloadExcel")
//    @ResponseBody
    public void downloadBloodDispenseExcel(HttpServletRequest request, HttpServletResponse response){

        //查询条件
        String number = request.getParameter("number");
        String inspectionSheet = request.getParameter("inspectionSheet");
        String medicalRecordNumber = request.getParameter("medicalRecordNumber");
        String name = request.getParameter("name");
        String bloodSampleNumber = request.getParameter("bloodSampleNumber");
        String bloodNumber = request.getParameter("bloodNumber");
        String dispenseMethod = request.getParameter("dispenseMethod");
        String dispenseRegister = request.getParameter("dispenseRegister");
        String dispenseRegisterNumber = request.getParameter("dispenseRegisterNumber");
        String dispensePeople = request.getParameter("dispensePeople");
        String dispensePeopleNumber = request.getParameter("dispensePeopleNumber");
        String reviewerNumber = request.getParameter("reviewerNumber");
        String reviewerName = request.getParameter("reviewerName");
        String startRegisterTime = request.getParameter("startRegisterTime");
        String endRegisterTime = request.getParameter("endRegisterTime");
        String startDispenseTime = request.getParameter("startDispenseTime");
        String endDispenseTime = request.getParameter("endDispenseTime");
        String startReviewTime = request.getParameter("startReviewTime");
        String endReviewTime = request.getParameter("endReviewTime");
        String statue = request.getParameter("statue");

        Result result = new Result();
        try {
            String filename = "交叉配血";
            String userAgent = request.getHeader("User-Agent");
            if(userAgent.contains("MSIE")||userAgent.contains("Trident")){
                filename = URLEncoder.encode(filename,"UTF-8");            }else {
                filename = new String(filename.getBytes("UTF-8"),"ISO-8859-1");
            }
            response.setContentType("application/json.ms-exce");
            response.setCharacterEncoding("utf-8");
            response.addHeader("Content-Disposition","filename = " + filename + ".xlsx");

          //创建查询条件
        QueryWrapper<BloodDispenseVo> queryWrapper = new QueryWrapper<>();
        //单据号
        if (number != "")
            queryWrapper.like("a.number", number);
        //检验单号
        if (inspectionSheet != "")
            queryWrapper.eq("inspection_sheet", inspectionSheet);
        //病历号
        if (medicalRecordNumber != ""){
            queryWrapper.like("medical_record_number", medicalRecordNumber);
        }
        //姓名
        if (name != "")
            queryWrapper.eq("name",name);
        //血液样本编号
        if (bloodSampleNumber != "")
            queryWrapper.like("blood_sample_number", bloodSampleNumber);
        //血液样本编号
//        if (bloodNumber != "")
//            queryWrapper.eq("blood_sample_id", bloodSample.getId());
        //配血基本信息登记人工号
        if (dispenseRegisterNumber != "")
            queryWrapper.like("dispense_register_number", dispenseRegisterNumber);
        //配血基本信息登记人
        if (dispenseRegister != "")
            queryWrapper.like("dispense_register", dispenseRegister);
        //配血基本信息登记时间
        if(startRegisterTime != "")
            queryWrapper.ge("dispense_register_time", startRegisterTime);
        if(endRegisterTime != "")
            queryWrapper.le("dispense_register_time", endRegisterTime);
        //配血人工号
        if (dispensePeopleNumber != "")
            queryWrapper.like("dispense_people_number", dispensePeopleNumber);
        //配血人
        if (dispensePeople != "")
            queryWrapper.like("dispense_people", dispensePeople);
        //配血时间
        if(startDispenseTime != "")
            queryWrapper.ge("dispense_time", startDispenseTime);
        if(endDispenseTime != "")
            queryWrapper.le("dispense_time", endDispenseTime);
        //审核人工号
        if (reviewerNumber != "")
            queryWrapper.like("a.reviewer_number", reviewerNumber);
        //审核人
        if (reviewerName != "")
            queryWrapper.like("a.reviewer_name", reviewerName);
        //审核时间
        if(startReviewTime != "")
            queryWrapper.ge("a.review_time", startReviewTime);
        if(endReviewTime != "")
            queryWrapper.le("a.review_time", endReviewTime);
        //配血方法
        if (dispenseMethod!= "")
            queryWrapper.eq("dispense_method", dispenseMethod);
        //状态
        if (statue!= "")
            queryWrapper.eq("statue", statue);

        BloodDispenseVo bloodDispenseVo=new BloodDispenseVo();
        //查询数据库
        List<BloodDispenseVo> bloodTestVoList=bloodDispenseMapper.selectBloodDispenseExcel(queryWrapper);

        EasyExcel.write(response.getOutputStream(),BloodDispenseVo.class).sheet("sheet").doWrite(bloodTestVoList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 换血
     * @param bloodDispenseVo
     * @return
     */
    @RequestMapping("/normal/changeBlood")
    @ResponseBody
    public Result chengeBlood(@RequestBody BloodDispenseVo bloodDispenseVo) {
        System.out.println(bloodDispenseVo.getId()+"确认换");

        Boolean success = null;
//        找到配血失败的血液
        BloodSample bloodSample=bloodSampleMapper.selectByNumber(bloodDispenseVo.getBloodNumber());
        BloodInformation bloodInformation=bloodInformationMapper.selectByBloodNumber(bloodSample.getBloodNumber());
        //配血失败的血液状态改为在库
        bloodInformation.setState("2");
        bloodSample.setState("001");
        //找到可以更换的血液
        List<BloodInformationVo> bloodInformations =
                bloodInformationMapper.selectChangeBlood(bloodInformation.getAboBloodType(),
                        bloodInformation.getRhBloodType(),bloodInformation.getBloodComponent(),bloodInformation.getBloodCount());
        if(bloodInformations.size()!=0) {
            Result result=new Result();
            //第一个就是要更换的血液
            BloodInformation bloodInformation1 = bloodInformations.get(0);
            System.out.println(bloodInformation1.getBloodId()+"hjghjhjjhb");
            //更换的血液样本编号
            BloodSample bloodSample1 = bloodSampleMapper.selectByBloodId(bloodInformation1.getBloodId());

            bloodInformation1.setState("10");
            bloodSample1.setState("004");

            //找到配血失败分配的数据
            TransfusionApplicationInformation transfusionApplicationInformation =
                    transfusionApplicationInformationMapper.selectByBloodNumber(bloodDispenseVo.getPatientId(),
                            bloodInformation.getBloodId());

            System.out.println(transfusionApplicationInformation.getId()+"赶紧回家");
            //拷贝一份
            //拷贝成BloodDispense
            TransfusionApplicationInformation transfusionApplicationInformation1 = new TransfusionApplicationInformation();

            BeanUtils.copyProperties(transfusionApplicationInformation, transfusionApplicationInformation1);
            transfusionApplicationInformation1.setBloodNumber(bloodInformation1.getBloodId());


            //拷贝一份bloodDispenseVo
            BloodDispenseVo bloodDispenseVo1 = new BloodDispenseVo();
            BeanUtils.copyProperties(bloodDispenseVo, bloodDispenseVo1);
            bloodDispenseVo1.setBloodNumber(bloodSample1.getNumber());
            bloodDispenseVo1.setStatue("1");
            bloodDispenseVo1.setDispensePeople("");
            bloodDispenseVo1.setDispensePeopleNumber("");
            bloodDispenseVo1.setMainSideResult("");
            bloodDispenseVo1.setPrimarySideResult("");
            bloodDispenseVo1.setReviewerName("");
            bloodDispenseVo1.setReviewerNumber("");
            bloodDispenseVo1.setBloodSampleId(bloodSample1.getId());

            //将bloodDispense1拷贝成bloodDispense
            BloodDispense bloodDispense = new BloodDispense();
            BeanUtils.copyProperties(bloodDispenseVo1, bloodDispense);

            //更新数据库
            bloodInformationService.updateById(bloodInformation);
            bloodInformationService.updateById(bloodInformation1);
            bloodSampleService.updateById(bloodSample);
            bloodSampleService.updateById(bloodSample1);
            transfusionApplicationInformationService.save(transfusionApplicationInformation1);
            transfusionApplicationInformationService.removeById(transfusionApplicationInformation.getId());
            bloodDispenseService.removeById(bloodDispenseVo.getId());
            success = bloodDispenseService.save(bloodDispense);
            //配血审核登记
            BloodDispenseReview bloodDispenseReview = new BloodDispenseReview();
            bloodDispenseReview.setBloodDispenseId(bloodDispense.getId());
//            bloodDispenseReview.setApplicantId(Long.parseLong(bloodDispenseVo1.getDispenseRegisterNumber()));
            bloodDispenseReview.setApplicantId(bloodDispenseVo1.getDispenseRegisterNumber());
            bloodDispenseReview.setApplyTime(bloodDispenseVo1.getDispenseRegisterTime());
            bloodDispenseReview.setState("1");
            bloodDispenseReviewService.save(bloodDispenseReview);

            if(success) {
                result.setCode("200");
                result.setMsg("换血成功");
            }else{
                result.setCode("500");
                result.setMsg("换血失败");
            }
            result.setCode("200");
            result.setData(bloodInformation1);
            System.out.println(bloodInformation1.getId());
            return result;

        }

        Result result1=new Result();

        return result1;
    }
    /**
     * 点击换血按钮
     * @param bloodDispenseVo
     * @return
     */
    @RequestMapping("/normal/chickChangeBlood")
    @ResponseBody
    public Result chengeBloodInormation(@RequestBody BloodDispenseVo bloodDispenseVo) {
        System.out.println(bloodDispenseVo.getId()+"换血按钮");

        //        找到配血失败的血液
        BloodSample bloodSample=bloodSampleMapper.selectByNumber(bloodDispenseVo.getBloodNumber());
        BloodInformation bloodInformation=bloodInformationMapper.selectByBloodNumber(bloodSample.getBloodNumber());
        //找到可以更换的血液
        List<BloodInformationVo> bloodInformations =
                bloodInformationMapper.selectChangeBlood(bloodInformation.getAboBloodType(),
                        bloodInformation.getRhBloodType(),bloodInformation.getBloodComponent(),bloodInformation.getBloodCount());
        System.out.println(bloodInformations.size());
        if(bloodInformations.size()!=0) {
            Result result=new Result();

            //第一个就是要更换的血液
            BloodInformationVo bloodInformationVo = bloodInformations.get(0);
            result.setCode("200");
            result.setData(bloodInformationVo);
            return result;
        }

        return new Result();
    }



    /**
     * 疑难配血
     * @param bloodDispense
     * @return
     */
    @RequestMapping("/normal/hard")
    @ResponseBody
    public Result hardBloodDispense(@RequestBody BloodDispense bloodDispense) {
        Result result = new Result();
        bloodDispense.setStatue("6");
        boolean success = bloodDispenseService.updateById(bloodDispense);
        if(success) {
            result.setCode("200");
            result.setMsg("操作成功");
        }else{
            result.setCode("500");
            result.setMsg("操作失败");
        }
        return result;
    }


}
